﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using UnityEditor;
using UnityEngine;

namespace State.Editor
{
    [CustomEditor(typeof(BaseContextBase), true)]
    public class StateEditor : UnityEditor.Editor
    {
        private SerializedObject mObj;
        private BaseContextBase ContextBaseObj;
        private List<Type> childType;

        private bool isEmptyRoot = true;
        private string RootName = "StateRoot";

        private void OnEnable()
        {
            mObj = new SerializedObject(target);
            ContextBaseObj = target as BaseContextBase;

            Type type = typeof(StateBase);
            childType = new List<Type>(type.Assembly.GetTypes().Where(a => a.BaseType == type));

            gUIContents = new GUIContent[childType.Count + 1];
            gUIContents[0] = new GUIContent("None");
            for (int i = 0; i < childType.Count; i++)
            {
                gUIContents[i + 1] = new GUIContent(childType[i].Name);
            }

            if (ContextBaseObj.transform.Find(RootName) != null) isEmptyRoot = false;
        }

        //private int childTypeSelectIndex;
        private GUIContent[] gUIContents;
        public override void OnInspectorGUI()
        {
            mObj.Update();
            base.DrawDefaultInspector();

            GUILayout.Space(10);
            if (gUIContents == null || gUIContents.Length <= 1)
            {
                EditorGUILayout.HelpBox(new GUIContent("未找到 GameStateBase 的子类"));
                return;
            }

            EditorGUILayout.LabelField("默认状态：");
            ContextBaseObj.childTypeSelectIndex = EditorGUILayout.Popup(ContextBaseObj.childTypeSelectIndex, gUIContents);
            mObj.ApplyModifiedProperties();


            if (GUILayout.Button("快速创建默认状态"))
            {
                if (ContextBaseObj.childTypeSelectIndex > 0)
                {
                    void SetIdleState()
                    {
                        GameObject go = null;
                        if (isEmptyRoot) go = new GameObject(RootName);
                        else go = ContextBaseObj.transform.Find(RootName).gameObject;
                        go.transform.parent = ContextBaseObj.transform;
                        GameObject stateObj = new GameObject(gUIContents[ContextBaseObj.childTypeSelectIndex].text);
                        StateBase idle = stateObj.AddComponent(childType[ContextBaseObj.childTypeSelectIndex - 1]) as StateBase;
                        idle.Context = ContextBaseObj;
                        ContextBaseObj.State = idle;
                        stateObj.transform.parent = go.transform;
                    }

                    if (ContextBaseObj.State != null)
                    {
                        if (EditorUtility.DisplayDialog("警告", "可能会覆盖原本的内容，你确定吗", "搞快点", "但是我拒绝"))
                            SetIdleState();
                    }
                    else
                    {
                        SetIdleState();
                    }
                }
                else
                {
                    Debug.LogError("请先选择默认状态");
                }
            }
        }
    }
}
